package com.enlorenz.core.channel.common.msgutil;

import com.enlorenz.common.util.StringUtils;

/**
 * 消息打包器
 * @author MAC
 */
public class MsgPacker {
	//头长度
	private int headLen=8;
	//消息载体
	private StringBuilder totalMsg=new StringBuilder();
	//编码
	private String encoding="utf-8";
	
	public MsgPacker(){
	}
	
	public MsgPacker(int headLen){
		this.headLen=headLen;
	}
	
	public MsgPacker(int headLen,String encoding){
		this.headLen=headLen;
		this.encoding=encoding;
	}
	
	
	/**
	 * 加入变长域
	 * @param msg
	 * @param fieldLength 域长度
	 */
	public void addVarField(String msg,int fieldLength) {
        try {
        	byte [] dataBuff=msg.getBytes(encoding);
    		String lenStr=String.valueOf(dataBuff.length);
			totalMsg.append(StringUtils.appendLeftBlank(lenStr, fieldLength,encoding));
			totalMsg.append(msg);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
    }
	
	/**
	 * 加入变长域
	 * @param msg
	 * @param fieldLength 域长度
	 */
	public void addVarField(String msg,int fieldLength,char symbol) {
        try {
        	byte [] dataBuff=msg.getBytes(encoding);
    		String lenStr=String.valueOf(dataBuff.length);
			totalMsg.append(StringUtils.appendLeftSymbol(lenStr, fieldLength, symbol,encoding));
			totalMsg.append(msg);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
    }
	
	
	/**
	 * 加入定长域
	 * @param msg	域内容
	 * @param length	长度
	 */
    public void addFixedField(String msg,int length) {
        try {
			totalMsg.append(StringUtils.appendLeftBlank(msg, length,encoding));
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
    }
    
    /**
     * 加入定长域
     * @param msg	域内容
     * @param length	长度
     * @param symbol	补齐符号
     */
    public void addFixedField(String msg,int length,char symbol) {
        try {
			totalMsg.append(StringUtils.appendLeftSymbol(msg, length, symbol,encoding));
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
    }
    
    /**
     * 获取消息
     * @return
     */
    public byte [] getTotalMsgBuff() { 
        try {
			return getTotalMsg().getBytes(encoding);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
    }
    
    /**
     * 获取消息
     * @return
     */
    public String getTotalMsg() { 
    	String str=totalMsg.toString();
        String head = null;
		try {
	        int totalLength=str.getBytes(encoding).length;
			head = StringUtils.appendLeftBlank(String.valueOf(totalLength),headLen,encoding);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
        return head+str;
    }
    
    public static void main(String[] args) {
		MsgPacker msg=new MsgPacker();
		msg.addFixedField("bbc", 4);
		msg.addVarField("mt", 4);
		msg.addVarField("mtb", 4);
    	
    	MsgParser par=new MsgParser(msg.getTotalMsgBuff());
    	par.analyzeFixedField("len", 8);
    	par.analyzeFixedField("k1", 4);
    	par.analyzeVarField("k2", 4);
    	par.analyzeVarField("k3", 4);
    	System.out.println(par);
    }
    
}
